package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Customer;
import com.itheima.domain.LinkMan;
import com.itheima.domain.UserInfo;
import com.itheima.service.CustomerService;
import com.itheima.service.LinkManService;
import com.itheima.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * ClassName:LinkManController
 * Package:com.itheima.controller
 * Description:
 * Date:2020/3/18 21:55
 * User: snow
 */
@Controller
@RequestMapping("/linkman")
public class LinkManController {
	@Autowired
	private LinkManService linkManService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 查找所有客户
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findAll.do")
	public ModelAndView findAll
	(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
	 @RequestParam(name = "size", required = true,defaultValue = "14")Integer size)throws Exception{
		ModelAndView view = new ModelAndView();
		List<LinkMan> linkManList = linkManService.findAll(page,size);
		// PageInfo是一个分页bean
		PageInfo pageInfo = new PageInfo(linkManList);
		view.addObject("pageInfo",pageInfo);
		view.setViewName("linkman/linkman-list");
		return view;
	}

	/**
	 * 模糊查询
	 * @param linkmanName
	 * @param linkmanGender
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findSome.do")
	public ModelAndView findSome
	(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
	 @RequestParam(name = "size", required = true,defaultValue = "14")Integer size,
	 String linkmanName,String linkmanGender) throws Exception {
		ModelAndView view = new ModelAndView();
		List<LinkMan> linkManList = linkManService.findSome(linkmanName,linkmanGender,page,size);
		// PageInfo是一个分页bean
		PageInfo pageInfo = new PageInfo(linkManList);
		view.addObject("pageInfo",pageInfo);
		view.addObject("linkmanGender",linkmanGender);
		view.addObject("linkManList",linkManList);
		view.setViewName("linkman/linkman-list");
		return view;
	}

	/**
	 * 创建联系人前加载基本信息
	 */
	@RequestMapping("/saveBefore.do")
	public ModelAndView saveBefore() throws Exception {
		ModelAndView view = new ModelAndView();
		// 查询客户列表
		List<Customer> customerList = customerService.findAll();
		view.addObject("customerList",customerList);
		view.setViewName("linkman/linkman-add");
		return view;
	}
	/**
	 * 动态查询客户所属和负责人 响应json
	 *
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectUserInfo.do")
	@ResponseBody
	public UserInfo selectUserInfo(String customerId) throws Exception {
		// 1、获得ajax请求
		//2、获取联系人
		Customer customer = customerService.findCustomerById(customerId);
		String userInfoId = customer.getUserInfo().getId();
		UserInfo userInfo = userInfoService.findUserInfoById(userInfoId);
		//3、返回客户所属员工信息
		return userInfo;
	}


	/**
	 * 创建联系人
	 * 通过姓名获取当前用户id，存入linkman的creatId
	 */
	@RequestMapping("/save.do")
	public String save(LinkMan linkMan,String userName) throws Exception {
		// 将当前用户id存储在客户对象中
		UserInfo creatInfo = userInfoService.findByName(userName);
		//获取当前用户id，送给creatInfo.id,传给creatId
		System.out.println(creatInfo.getId());
		linkMan.setCreatInfo(creatInfo);
		// 创建Date对象
		// 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
		// 执行Service层中的创建方法，返回的是受影响的行数
		linkManService.save(linkMan);
		return "redirect:findAll.do";
	}

	/**
	 * 删除联系人
	 */
	@RequestMapping("/delete.do")
	@RolesAllowed({"ADMIN", "MANAGER"})
	public String delete(String id) throws Exception {
		linkManService.delete(id);
		return "redirect:findAll.do";
	}


	/**
	 * 查找客户详细信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById.do")
	public ModelAndView findById(String id) throws Exception {
		ModelAndView view = new ModelAndView();
		LinkMan linkMan = linkManService.findById(id);
		view.addObject("linkMan", linkMan);
		view.setViewName("linkman/linkman-show");
		return view;
	}

	/**
	 * 更改联系人前查找详细信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateBefore.do")
	public ModelAndView updateBefore(String id) throws Exception {
		ModelAndView view = new ModelAndView();
		//查找所有客户信息
		List<Customer> customers = customerService.findAll();
		view.addObject("customerList",customers);
		//根据id查找联系人信息
		LinkMan linkMan = linkManService.findById(id);
		view.addObject("linkMan", linkMan);
		//跳转到联系人信息修改界面
		view.setViewName("linkman/linkman-update");
		return view;
	}

	/**
	 * 更改联系人前查找详细信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update.do")
	public String update(LinkMan linkMan) throws Exception {
		System.out.println(linkMan);
		linkManService.update(linkMan);
		return "redirect:findAll.do";
	}




}
